<template>
  <div>
    <el-form ref="summaryEditForm" :model="formItem" label-width="120px" size="mini">
      <el-row :gutter="40">
        <el-col :span="8">
          <el-form-item label="服务编号" prop="registerCode">
            <el-input v-model="registerCode" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="巡检厅局" prop="unitName">
            <el-input v-model="unitName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="执行人" prop="reportPersonName">
            <el-input v-model="formItem.reportPersonName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开始时间" prop="startTime">
            <el-date-picker
              v-model="formItem.startTime"
              disabled
              type="date"
              placeholder="选择开始时间"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束时间" prop="endTime">
            <el-date-picker
              v-model="formItem.endTime"
              disabled
              type="date"
              placeholder="选择结束时间"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="服务总结" prop="record">
            <el-input
              v-model="formItem.record"
              placeholder="请填写服务总结"
              type="textarea"
              :autosize="{ minRows: 3, maxRows: 3 }"
              resize="none"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="上传附件">
            <el-upload
              class="upload-demo"
              :headers="headers"
              :action="importUrl"
              :file-list="formItem.files"
              :before-upload="handleBeforeUpLoad"
              :on-success="handleSuccess"
              :on-error="handleError"
              :on-remove="handleRemove"
              :show-file-list="true"
            >
              <el-button size="small" type="primary" icon="el-icon-upload2">上传</el-button>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { summaryDetail, summaryUpdate, summaryUpload, deletefiles } from '@neu/dcom-ui/api/insuranceRegisterTask'
import { getToken } from '@neu/biz-core/utils/auth'
// 定义一个表单原始项
const formItem = {
  code: '',
  reportPersonCode: '',
  reportPersonName: '',
  unitCode: '',
  startTime: '',
  endTime: '',
  status: '',
  record: '',
  files: []
}
export default {
  name: 'SummaryEditForm',
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    },
    registerCode: {
      required: false,
      type: String,
      default: ''
    },
    unitName: {
      required: false,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      /** 上传路径 */
      importUrl: summaryUpload,
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Authentication': getToken()
      }
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: {
      handler() {
        if (this.id) {
          this.fetch()
        }
      },
      immediate: true
    }
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        summaryDetail(this.id).then(response => {
          console.log(response.data)
          this.formItem = {
            ...response.data
          }
          this.formItem.reportPersonName = this.$store.state.user.name
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      console.log('保存')
      console.log(this.formItem)
      const updated = this.id !== -1
      return new Promise(resolve => {
        this.$refs.summaryEditForm.validate((valid) => {
          if (valid) {
            if (updated) {
              resolve(summaryUpdate(this.id, this.formItem))
            }
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.summaryEditForm.resetFields()
      this.formItem = { ...formItem }
    },
    /** 文件上传成功 */
    handleSuccess(response, file, fileList) {
      if (response.status === 'failure') {
        this.$message({
          showClose: true,
          message: response.data,
          type: 'error'
        })
      } else {
        this.$message({
          showClose: true,
          message: '文件上传成功',
          type: 'success'
        })
        const obj = {
          fsId: response.data.fsId,
          name: response.data.name
        }
        const fsIdArr = this.formItem.files.map(t => t.fsId)
        if (!fsIdArr.includes(obj.fsId)) {
          this.formItem.files.push(obj)
        } else {
          fileList.pop()
        }
      }
    },
    /** 文件上传失败 */
    handleError(err, file, fileList) {
      console.log(err)
    },
    /** 文件移除 */
    handleRemove(file, fileList) {
      deletefiles(file.fsId).then(res => {
        if (res.status === 'failure') {
          this.$message({
            showClose: true,
            message: res.data,
            type: 'error'
          })
        } else {
          this.formItem.files.splice(this.formItem.files.indexOf(file), 1)
          this.$message({
            showClose: true,
            message: '删除成功',
            type: 'success'
          })
        }
      })
    },
    /** 文件上传之前 */
    handleBeforeUpLoad(file) {

    }
  }
}
</script>

